package com.fenxiangle.yueding.feature.publish.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.common.util.DateTool;
import com.fenxiangle.yueding.entity.bo.OrderBo;
import com.fenxiangle.yueding.entity.bo.PublishDemandBo;
import com.fenxiangle.yueding.feature.home.view.SelectMapActivity;
import com.fenxiangle.yueding.feature.mine.view.RechargeCreditActivity;
import com.fenxiangle.yueding.feature.publish.contract.PublishContract;
import com.fenxiangle.yueding.feature.publish.dependencies.mine.DaggerPublishComponent;
import com.fenxiangle.yueding.feature.publish.dependencies.mine.PublishPresenterModule;
import com.suozhang.framework.framework.BaseActivity;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.fenxiangle.yueding.common.util.DateTool.DEFAULT_SDF3;

/**
 * Created by Home on 2018/9/1.
 */

public class PublishInviteActivity extends BaseActivity implements PublishContract.View {
    @Inject
    PublishContract.Presenter mPresenter;

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.edt_publish_invite_descrption)
    EditText mEdtPublishInviteDescrption;
    @BindView(R.id.tv_publish_invite_starttime)
    TextView mTvPublishInviteStarttime;
    @BindView(R.id.tv_publish_invite_endtime)
    TextView mTvPublishInviteEndtime;
    @BindView(R.id.tv_publish_invite_address)
    TextView mTvPublishInviteAddress;
    @BindView(R.id.tv_publish_invite_money)
    EditText mTvPublishInviteMoney;
    private PoiItem mPoiItem;
    private String mUserUid;
    private String mOccupation;
    private String mCoordinate;


    private PublishDemandBo publishDemandBo;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_publish_invite;
    }

    @Override
    protected void initInjector() {
        DaggerPublishComponent.builder().publishPresenterModule(new PublishPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {
        initToolBar(toolbar, "邀约", true, true);
        mUserUid = this.getIntent().getStringExtra("uid");
        mOccupation = getIntent().getStringExtra("pos");
        publishDemandBo = (PublishDemandBo) this.getIntent().getSerializableExtra("data");
        if (publishDemandBo != null) {
            mEdtPublishInviteDescrption.setText(publishDemandBo.getOrderDescribe());
            mTvPublishInviteMoney.setText(publishDemandBo.getTotalPrice() + "");
            mTvPublishInviteAddress.setText(publishDemandBo.getAgreedPlace());
            mCoordinate=publishDemandBo.getCoordinate();
            mUserUid=publishDemandBo.getUserUid();

        }
    }

    @OnClick({R.id.tv_publish_invite_starttime, R.id.tv_publish_invite_endtime, R.id.tv_publish_invite_address, R.id.btn_publish_invite_})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_publish_invite_starttime:
                TimePickerView pvStart = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        mTvPublishInviteStarttime.setText(DateTool.date2String(date, DEFAULT_SDF3));
                    }
                })
                        .setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                        .setLineSpacingMultiplier(1.8f)
                        .build();
                pvStart.show();
                break;

            case R.id.tv_publish_invite_endtime:
                TimePickerView pvEnd = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        mTvPublishInviteEndtime.setText(DateTool.date2String(date, DEFAULT_SDF3));
                    }
                })
                        .setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                        .setLineSpacingMultiplier(1.8f)
                        .build();
                pvEnd.show();
                break;
            case R.id.tv_publish_invite_address:
                startActivityForResult(new Intent(this, SelectMapActivity.class), 3);
                break;
            case R.id.btn_publish_invite_:

                mPresenter.publishInvite(2,
                        mUserUid,
                        mEdtPublishInviteDescrption.getText().toString(),
                        mTvPublishInviteStarttime.getText().toString(),
                        mTvPublishInviteEndtime.getText().toString(),
                        mTvPublishInviteAddress.getText().toString(),
                        mCoordinate,
                        mTvPublishInviteMoney.getText().toString()
                );
                break;
        }
    }

    @Override
    public void showPublishSuccess(OrderBo s) {
        Intent intent = new Intent(this, PayActivity.class);
        intent.putExtra("no", s);
        startActivity(intent);
        finish();
    }

    @Override
    public void showError(String error) {
        showMsg(error);
    }

    @Override
    public void showInsufficientBalance() {
        startActivity(new Intent(this, RechargeCreditActivity.class));
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 3:
                if (data != null) {
                    mPoiItem = data.getParcelableExtra("poi_item");
                    if (mPoiItem != null) {
                        mTvPublishInviteAddress.setText(mPoiItem.getTitle() + "(" + mPoiItem.getSnippet() + ")");
                        mCoordinate = mPoiItem.getLatLonPoint().getLatitude() + "," + mPoiItem.getLatLonPoint().getLongitude();
                    }
                }
                break;
        }
    }

    @Override
    public void showPublishListSuccess(List<PublishDemandBo> workBoPageData) {

    }

    @Override
    public void showPublishListError(String error) {

    }

    @Override
    public void showPublishListMoreSuccess(List<PublishDemandBo> msg) {

    }

    @Override
    public void showPublishListMoreError(String msg) {

    }

    @Override
    public void showPublishListEmpty() {

    }

    @Override
    public void showcancleOrderSuccess() {

    }

    @Override
    public void showConfrimOrderSuccess() {

    }

    @Override
    public void showConfrimTxSuccess() {

    }

    @Override
    public void showWeiQunanOrderSuccess() {

    }
}
